package com.logica.mdm.webservices.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.logica.mdm.webservices.dao.SearchPartyContractDao;
import com.logica.mdm.webservices.dao.beans.PartyContractResultBean;

/**
 * Implementation using NamedParameterJdbcTemplate
 */
@Repository
public class JdbcSearchPartyContractDaoImpl implements SearchPartyContractDao{

	private NamedParameterJdbcTemplate jdbcTemplate;

	@Autowired
	public void setDataSource(DataSource dataSource) {
	    this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<PartyContractResultBean> searchPartyContract(String sqlQuery, Map<String, ?> sqlParams) {
		return this.jdbcTemplate.query(sqlQuery, sqlParams, new PartyContractMapper());
	}

	/**
	 * Inner class defining the mapping between the database and PartyContractResultBean
	 */
	private static final class PartyContractMapper implements RowMapper<PartyContractResultBean> {
		public PartyContractResultBean mapRow(ResultSet rs, int rowNum) throws SQLException {
			PartyContractResultBean contract = new PartyContractResultBean();
			contract.setContractNumber(rs.getString("NUMERO_CONTRAT"));
			contract.setContractOffice(rs.getString("CONTRACT_OFFICE"));
			contract.setMdmId(rs.getString("PARTY_ID"));
	        return contract;
	    }
	}
}
